﻿using FANUC.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class PlantSafetyDaysController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        [ResponseType(typeof(PlantSafetyDaysDTO))]
        public IHttpActionResult Get()
        {
            #region Test
            bool test = true;
            if (test)
            {
                PlantSafetyDaysDTO testData = new PlantSafetyDaysDTO();
                testData.LastIssueHappenedDay = System.DateTime.Now.AddDays(-100);
                testData.Today = System.DateTime.Now.Date;
                return Ok(testData);
            }
            #endregion

            var lastIssue = db.Plant_Issue.OrderByDescending(a => a.ID).FirstOrDefault();
            if (lastIssue == null)
            {
                PlantSafetyDaysDTO data = new PlantSafetyDaysDTO();
                data.LastIssueHappenedDay = System.DateTime.Now.Date.AddDays(-1);
                data.Today = System.DateTime.Now.Date;
                return Ok(data);
            }
            else
            {
                PlantSafetyDaysDTO data = new PlantSafetyDaysDTO();
                data.LastIssueHappenedDay = lastIssue.HappenedDateTime.Date;
                data.Today = System.DateTime.Now.Date;
                return Ok(data);
            }
        }
    }
}
